import Vue from 'vue'
import VueRouter from 'vue-router'
import homeView from '../views/homeView.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "homeView"
  },
  {
    path: '/homeView',
    name: 'homeView',
    component: homeView,
  },
  {
    path: '/classRoomView',
    name: 'classRoomView',
    component: ()=>import('../views/classRoomView.vue'),
  },
  {
    path: '/collectionView',
    name: 'collectionView',
    component: ()=>import('../views/collectionView.vue'),
  },
  {
    path: '/PersonalView',
    name: 'PersonalView',
    component: ()=>import('../views/PersonalView.vue'),
  },
  {
    path: '/detailsView',
    name: 'detailsView',
    component: ()=>import('../views/detailsView.vue'),
  },
  {
    path: '/allWorksView',
    name: 'allWorksView',
    component: ()=>import('../views/allWorksView.vue'),
  },
  {
    path: '/commentView',
    name: 'commentView',
    component: ()=>import('../views/commentView.vue'),
  },
  {
    path: '/signInview',
    name: 'signInview',
    component: ()=>import('../views/signInview.vue'),
  },
  {
    path: '/setupView',
    name: 'setupView',
    component: ()=>import('../views/setupView.vue'),
  },
  {
    path: '/favoritesMenu',
    name: 'favoritesMenu',
    component: ()=>import('../views/favoritesMenu.vue'),
  },
  {
    path: '/historyView',
    name: 'historyView',
    component: ()=>import('../views/historyView.vue'),
  },
  {
    path: '/searchView',
    name: 'searchView',
    component: ()=>import('../views/searchView.vue'),
  },
  {
    path: '/classificationView',
    name: 'classificationView',
    component: ()=>import('../views/classificationView.vue'),
  },
  {
    path: '/establishView',
    name: 'establishView',
    component: ()=>import('../views/establishView.vue'),
  },
  {
    path: '/myCaidan',
    name: 'myCaidan',
    component: ()=>import('../views/myCaidan.vue'),
  }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  let token = window.sessionStorage.getItem("token");

  // 登录界面判断
  if(to.path == "/signInview"){
      next();
      return;
  }else{ //判断是否登录
    if(!token){
      next("/signInview");//进入登录页面
      return;
    }else{
      next();//执行下一个路由
    }
  }
})

export default router
